package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrdersettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Transactional
@Service(interfaceClass = OrdersettingService.class)
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrdersettingDao ordersettingDao;

    @Override
    public void add(List<OrderSetting> orderSettingList) {

        orderSettingList.stream().forEach(orderSetting -> {
            //判断当前日期在数据库中是否存在
            int count=ordersettingDao.findCountByDate( orderSetting.getOrderDate());

            if(count>0){
                //如果存在则更新预约人数
                ordersettingDao.updateNumberByDate(orderSetting);
            }else{
                //如果不存则添加
                ordersettingDao.add(orderSetting);
            }


        });


    }


    @Override
    public List<Map> getOrdersettingByMonth(Date date) {
        return ordersettingDao.getOrdersettingByMonth(date);
    }


    @Override
    public void updateNumberByDate(OrderSetting orderSetting) {
        //判断当前日期在数据库中是否存在
        int count=ordersettingDao.findCountByDate( orderSetting.getOrderDate());

        if(count>0){
            //如果存在则更新预约人数
            ordersettingDao.updateNumberByDate(orderSetting);
        }else{
            //如果不存则添加
            ordersettingDao.add(orderSetting);
        }
    }

    @Override
    public List<Map> getOrdersettingByMonth1(String date) {

        String begin=date+"-01";
        String end=date+"-31";

        List<Map> list=  ordersettingDao.getOrdersettingByMonth1(begin,end);

        return list;
    }
}
